const searchForm = document.getElementById('search-form');
const searchBtn = document.getElementById('search-button');
const searchText = document.getElementById('search-box');
searchBtn.parentElement.addEventListener('mouseover', () => {
	if (!searchBtn.parentElement.classList.contains('is-focused')) {
		searchBtn.parentElement.classList.add('is-focused');
	}
	searchText.focus();
});
searchBtn.parentElement.addEventListener('mouseleave', () => {
	if (searchText.value === "") {
		searchBtn.parentElement.classList.remove('is-focused');
	}
});
searchBtn.addEventListener('click', () => {
	if (searchText.value !== "") {
		searchForm.submit();
	}
});
searchForm.addEventListener('submit', () => {
	//查看作品
	let id = searchText.value;
	if (id.length === 3) {
		id = '16180600' + id;
	}
	window.location.href = 'user.html?id=' + id;
});
//加载列表
const loadingPageButton = document.getElementById('load-more');
let currentPage = 1;
function loadList(page) {
	loadingPageButton.classList.add('loading');
	ajax('/indexshow.do?page=' + page)
	.then(r => JSON.parse(r))
	.then(r => {
		r.data.forEach(e => {
			let n = getTemplate('item');
			n.querySelector('.title').appendChild(document.createTextNode(e.atype));
			n.querySelector('.author').appendChild(document.createTextNode(e.author.join(' ')));
			n.querySelector('.time').appendChild(document.createTextNode(e.uptime));
			n.querySelector('.view-link').href = 'view.html?url=' + e.url;
			document.querySelector('.works-list').appendChild(n);
		});
		loadingPageButton.classList.remove('loading');
	});
}
loadingPageButton.addEventListener('click', function() {
	if (this.classList.contains('loading')) {
		return;
	}
	loadList(++currentPage);
});
loadList(currentPage);